﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="JDataController.cs" company="">
//   
// </copyright>
// <summary>
//   Defines the JDataController type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace OneAdmin.Controllers.Data
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.Mvc;

    using OneAdmin.Models;

    using WebCommon;
    using WebCommon.Extensions;

    public class JDataController : Controller
    {
        /// <summary>
        /// 获取数据/分页
        /// </summary>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <returns></returns>
        public ActionResult List(int page, int rows)
        {
            List<Test> tests = this.getList();
            PageList<Test> pl = new PageList<Test>(tests, 3);

            pl.StartPages(rows, page);

            return new JsonReturn(pl);
        }

        /// <summary>
        /// 添加model
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Add(Test model)
        {
            return this.Json(model, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 数据源
        /// </summary>
        /// <returns></returns>
        List<Test> getList()
        {
            var tests = new List<Test>();
            for (int i = 0; i < 500; i++)
            {
                tests.Add(new Test()
                {
                    Id = i,
                    Name = "name" + i
                });
            }
            return tests;
        }
        /// <summary>
        /// 修改项
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(int id)
        {

            var t = new Test  { Id = id, Name = "name" + id, ts = "asd" };

            return new JsonReturn(t); ;
        }
        /// <summary>
        /// 即时搜索
        /// </summary>
        /// <param name="wd"></param>
        /// <returns></returns>
        public ActionResult Sousuo(string wd)
        {
            var tests = getList().Select(x => x.Name).Where(x => x.Contains(wd));
            IEnumerable<Test> enumerable = getList().Where(x => x.Name.Contains(wd));
            return Json(enumerable, JsonRequestBehavior.AllowGet);
        }

        public string Del()
        {
            return "0";
        }

        public ActionResult Up()
        {
            return View();
        }

    }
}
